﻿using SimpleInjector;
using Sustainalytics.DataLayer.PI;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ProductInvolvement.BL;
using Sustainalytics.MongoDB.Extensions;
using System.Configuration.Abstractions;
using System.Web.Http;
using MongoDB.Driver;
using Sustainalytics.DataLayer;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.Utils;
using Sustainalytics.Claims;

namespace Sustainalytics.ProductInvolvement.Service.Infrastructure
{
    public static class CompositionRoot
    {
       
        public static Container RegisterDependencies(HttpConfiguration config)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();
            container.RegisterWebApiControllers(config);

            var clientPlatformConnectionString = ConfigurationManager.Instance.ConnectionStrings["ClientPlatform"].ConnectionString;

            container.RegisterMisc();
            
            container.RegisterCollections();

            container.RegisterRepositories(clientPlatformConnectionString);

            container.RegisterBussinessLogic(
                clientPlatformConnectionString);

            container.Verify();
            return container; 
        }

        private static void RegisterCollections(this Container container)
        {
            container.Register(() => MongoFactory.GetCollectionFromDatabase<CorporateData>(MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(typeof(IRepositoryCollection<CorporateData>), () => new RepositoryCollection<CorporateData>(MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(typeof(IRepositoryCollection<ProductInvolvementData>), () => new RepositoryCollection<ProductInvolvementData>(MongoFactory.GetConnectionStrings().ProductInvolvementDW));
           
        }

        private static void RegisterRepositories(this Container container, string clientPlatformConnectionString)
        {
            container.Register(typeof(IClientDwRepository), () => ClientDwRepository.GetInstance(MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(typeof(IProductInvolvementFrameworkRepository), () => new ProductInvolvementFrameworkRepository(MongoFactory.GetConnectionStrings().ProductInvolvementDW));
            container.Register(typeof(IProductInvolvementDataRepository), () => new ProductInvolvementDataRepository(MongoFactory.GetConnectionStrings().ProductInvolvementDW, MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(typeof(IFileRepository), () => new FileRepository(clientPlatformConnectionString));
            container.Register(() => CompanyScreeningRepository.GetCompanyScreeningRepository(clientPlatformConnectionString));

        }

        private static void RegisterBussinessLogic(this Container container,
            string clientPlatformConnectionString)
        {
            container.Register(typeof(IProductInvolvementBussinesLogic), () =>
                ProductInvolvementBussinesLogic.CreateProductInvolvementBussinesLogic(
                container.GetInstance<IFileRepository>(),
                container.GetInstance<IProductInvolvementDataRepository>(),
                container.GetInstance<IClientDwRepository>(),
                container.GetInstance<IProductInvolvementFrameworkRepository>(),
                container.GetInstance<ICompanyScreeningRepository>(),
                container.GetInstance<ProductInvolvementSerilog>(),
                container.GetInstance<IMongoCollection<CorporateData>>(),
                container.GetInstance<IRepositoryCollection<CorporateData>>(),
                container.GetInstance<IRepositoryCollection<ProductInvolvementData>>()));

            container.Register(typeof(ProductInvolvementPDFReportLogicWithTables), () => new ProductInvolvementPDFReportLogicWithTables(ProductInvolvementSerilog.Log)); //intrebat Mihai

        }
        private static void RegisterMisc(this Container container)
        {
            container.Register(() => MongoFactory.GetConnectionStrings());
            container.RegisterSingleton(typeof(IClaimsChecker), () => new ClaimsChecker());
            container.RegisterSingleton(typeof(ProductInvolvementSerilog), () => ProductInvolvementSerilog.Log);
        }
    }
  }




